package com.bawei.lib_common.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bawei.lib_common.R;

import java.util.Random;


public class RedPacketWidget extends LinearLayout {

    private static final String TAG = "RedPacketWidget";
    private TextView tvNum;
    private RedPacketView redPacketView;
    private TranslateAnimation translateAnimation;
    private Handler mhandler = new Handler();
    private Random random = new Random();


    public RedPacketWidget(Context context) {
        super(context,null);
        initView(context);
    }

    public RedPacketWidget(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr,0);
        initView(context);
    }

    public RedPacketWidget(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs,0);
        initView(context);
    }

    public RedPacketWidget(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_redpackwidget,null,false);
        tvNum = view.findViewById(R.id.tv_redpacket_num);
        redPacketView = view.findViewById(R.id.rpv_redpacket);
        addView(view);

        tvNum.setVisibility(INVISIBLE);
        Log.d(TAG, "RedPacketWidget initialized, tvNum: " + (tvNum != null) + ", redPacketView: " + (redPacketView != null));
        
        if (tvNum != null) {
            Log.d(TAG, "TextView initial visibility: " + tvNum.getVisibility() + ", text: " + tvNum.getText());
        }

        translateAnimation = new TranslateAnimation(0.0F,0.0F,0.0F,-70.0F);
        translateAnimation.setFillAfter(true);
        //动画执行时间
        translateAnimation.setDuration(2000);


        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.d(TAG, "Animation started, setting tvNum visible");
                tvNum.setVisibility(VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.d(TAG, "Animation ended, setting tvNum invisible");
                tvNum.setVisibility(INVISIBLE);
                // 动画结束后重置进度满标志
                if (redPacketView != null) {
                    redPacketView.resetProgressFullFlag();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        
        // 设置RedPacketView的回调，确保滑动时也能触发随机红包显示
        if (redPacketView != null) {
            redPacketView.setCallback(new RedPacketView.Callback() {
                @Override
                public void start() {
                    Log.d(TAG, "RedPacketView animation started");
                }

                @Override
                public void pause(int angle) {
                    Log.d(TAG, "RedPacketView animation paused at angle: " + angle);
                }

                @Override
                public void resume() {
                    Log.d(TAG, "RedPacketView animation resumed");
                }

                @Override
                public void completed() {
                    Log.d(TAG, "RedPacketView animation completed");
                    // 结束时的处理
                    if (stateCallback != null) {
                        stateCallback.completed();
                    }
                }
                
                @Override
                public void onProgressFull() {
                    // 进度满时显示随机红包数字
                    Log.d(TAG, "Progress full callback triggered from RedPacketView");
                    showRandomRedPacketNumber();
                }
            });
            Log.d(TAG, "RedPacketView callback set successfully");
        } else {
            Log.e(TAG, "RedPacketView is null, cannot set callback");
        }
    }
    
    /**
     * 滑动开始时的回调
     */
    public void onScrollStart() {
        if (redPacketView != null) {
            redPacketView.onScrollStart();
        }
    }
    
    /**
     * 滑动过程中的回调
     */
    public void onScroll() {
        if (redPacketView != null) {
            redPacketView.onScroll();
        }
    }
    
    /**
     * 滑动停止时的回调
     */
    public void onScrollStop() {
        if (redPacketView != null) {
            redPacketView.onScrollStop();
        }
    }
    
    /**
     * 生成随机红包数字
     * @return 随机红包数字
     */
    private int generateRandomRedPacketNumber() {
        // 生成1-999之间的随机数
        return random.nextInt(999) + 1;
    }
    
    /**
     * 显示随机红包数字
     */
    public void showRandomRedPacketNumber() {
        int randomNumber = generateRandomRedPacketNumber();
        Log.d(TAG, "Showing random red packet number: " + randomNumber);
        
        setTvNum(randomNumber);
        
        // 确保在主线程中执行动画
        post(new Runnable() {
            @Override
            public void run() {
                if (tvNum != null) {
                    Log.d(TAG, "Starting animation for number: " + randomNumber + ", tvNum visibility: " + tvNum.getVisibility());
                    tvNum.startAnimation(translateAnimation);
                    Log.d(TAG, "Started animation for number: " + randomNumber);
                } else {
                    Log.e(TAG, "tvNum is null, cannot start animation");
                }
            }
        });
        
        // 通知回调
        if (stateCallback != null) {
            Log.d(TAG, "Notifying stateCallback with amount: " + randomNumber);
            stateCallback.onRandomRedPacketGenerated(randomNumber);
        } else {
            Log.e(TAG, "stateCallback is null!");
        }
    }
    
    /**
     * 开始执行动画（保留用于手动触发，如果需要的话）
     */
    public void start(){
        redPacketView.startAnimate();
    }
    private StateCallback stateCallback;

    public void setStateCallback(StateCallback stateCallback) {
        this.stateCallback = stateCallback;
    }

    /**
     * 设置文本提示信息
     * @param num
     */
    public void setTvNum(int num){
        if (tvNum != null) {
            tvNum.setText("+" + num);
            Log.d(TAG, "Set tvNum text to: +" + num);
        } else {
            Log.e(TAG, "tvNum is null, cannot set text");
        }
    }

    /**
     * 停止动画
     */
    public void stop(){
        if (redPacketView != null) {
            redPacketView.stop();
        }
        if (tvNum != null) {
            tvNum.clearAnimation();
            tvNum.setVisibility(INVISIBLE);
        }
        if (mhandler != null) {
            mhandler.removeCallbacksAndMessages(null);
        }
    }

    /**
     * 状态回调接口
     */
    public interface StateCallback{
        void completed();
        void onRandomRedPacketGenerated(int amount); // 随机红包生成回调
    }
}






























